package cn.smbms.controller;

import cn.smbms.pojo.User;
import cn.smbms.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Resource
    private UserService userService;


    private Logger logger = Logger.getLogger(LoginController.class);

    @RequestMapping(value = "/login.html",method = RequestMethod.GET)
    public String toLogin() {
        return "login";
    }

    @RequestMapping(value = "/logout.html",method = RequestMethod.GET)
    public String loginOut(HttpSession session) {
        session.removeAttribute("userSession");
        return "redirect:/login.html";
    }

    @RequestMapping(value = "/dologin.html", method = RequestMethod.POST)
    public String doLogin(@RequestParam String userCode,
                          @RequestParam String userPassword,
                          HttpSession session,
                          HttpServletRequest request) {
        //调用service方法
        User user = userService.login(userCode, userPassword);
        if (user == null) {//登录失败
            request.setAttribute("error","登录失败！用户名或密码错误！");
            return "redirect:login.html";
        }
        session.setAttribute("userSession",user);
        return "redirect:main.html";
    }

    @RequestMapping(value = "/main.html")
    public String main(){
        return "frame";
    }


}
